package com.gg.user.contorller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.cart.service.CartService;
import com.gg.entity.User;
import com.gg.user.service.UserService;

import helpers.baseservlet.BaseServlet;
import povos.CartFruitPovo;

@WebServlet("/user/LoginUserServlet")
public class LoginUserServlet extends BaseServlet {
	
	@Autowired
	private UserService userService;
	@Autowired
	private CartService cartService;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String  emailortel = request.getParameter("emailortel");
		String password =request.getParameter("password");
		User user =new User();
		user.setPassword(password);
		if(emailortel.contains("@")) {
			user.setEmail(emailortel);
			
		}else {
			user.setTel(emailortel);
			
		}
		
		User u=userService.login(user);
		if(u==null) {
			request.setAttribute("code","err");
			request.setAttribute("msg","登录失败，请检查emailh/tel格式，密码是否正确");
			
			
		}else {
			
			List<CartFruitPovo> povos=cartService.getCartFruitPovoByUid(u.getUid());
			request.getSession().setAttribute("cartpovos",povos);
			
			request.getSession().setAttribute("user",u);
			request.setAttribute("code", "suc");
			request.setAttribute("msg", "登录成功！");
			
		}
		request.getRequestDispatcher("/msg.jsp").forward(request, response);
	}
}
